﻿<template>
  <div class="l-rblock l-form-viewer erpCase" style="padding: 8px">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="right"
      labelWidth="80px"
      ref="form"
    >
      <div class="l-auto-window only-tabs">
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane
            class="l-tabs-container"
            :label="$t('主表信息')"
            name="tab0"
          >
            <el-row :gutter="16">
              <div v-if="setBlock" class="right_right header_name">
                {{ $t("基本信息") }}
              </div>
              <div class="l-rblock">
                <div v-if="setBlock" class="form_top_upload">
                  <el-col :span="12">
                    <el-form-item
                      :label="$t('客户名称')"
                      prop="case_erp_customer_f_Name"
                    >
                      <el-input
                        v-model="formData.case_erp_customer_f_Name"
                        :placeholder="$t('please enter')"
                      >
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item
                      :label="$t('客户类型')"
                      prop="case_erp_customer_f_Type"
                    >
                      <l-select
                        :options="case_erp_customer_f_TypeOptions"
                        v-model="formData.case_erp_customer_f_Type"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item
                      :label="$t('所在行业')"
                      prop="case_erp_customer_f_Industry"
                    >
                      <el-input
                        v-model="formData.case_erp_customer_f_Industry"
                        :placeholder="$t('please enter')"
                      >
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item
                      :label="$t('来源')"
                      prop="case_erp_customer_f_Source"
                    >
                      <l-select
                        :options="case_erp_customer_f_SourceOptions"
                        v-model="formData.case_erp_customer_f_Source"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item
                      :label="$t('规模')"
                      prop="case_erp_customer_f_Scale"
                    >
                      <l-select
                        :options="case_erp_customer_f_ScaleOptions"
                        v-model="formData.case_erp_customer_f_Scale"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item
                      :label="$t('公司性质')"
                      prop="case_erp_customer_f_ComNature"
                    >
                      <l-select
                        :options="case_erp_customer_f_ComNatureOptions"
                        v-model="formData.case_erp_customer_f_ComNature"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item
                      :label="$t('地址')"
                      prop="case_erp_customer_f_Address"
                    >
                      <el-input
                        v-model="formData.case_erp_customer_f_Address"
                        :placeholder="$t('please enter')"
                      >
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item
                      :label="$t('图片上传')"
                      prop="case_erp_customer_f_File"
                    >
                      <l-upload
                        maxSize="2"
                        sizeType="MB"
                        :limit="9"
                        v-model="formData.case_erp_customer_f_File"
                        listType="picture-card"
                        :placeholder="$t('please enter')"
                      />
                    </el-form-item>
                  </el-col>
                </div>
                <el-col :span="24">
                  <div class="form_flex">
                    <div class="l-title form_name">{{ $t("联系人") }}</div>
                    <el-button
                      type="text"
                      @click="handleCase_erp_customercontactsAddRow"
                      icon="el-icon-plus"
                      >{{ $t("增加") }}</el-button
                    >
                  </div>

                  <div
                    v-for="(item, index) in case_erp_customercontacts_data"
                    :key="index"
                  >
                    <div class="form_flex" style="padding-left: 8px">
                      <div class="form_subtitle">
                        {{ $t("联系人" + (index + 1)) }}
                      </div>
                      <el-radio :label="index" v-model="index_f_IsDefault">
                        {{ $t("默认") }}
                      </el-radio>
                    </div>
                    <div class="mt-16">
                      <el-col :span="12">
                        <el-form-item
                          :label="$t('姓名')"
                          labelWidth="60px"
                          :rules="{
                            required: true,
                            message: '姓名不能为空',
                          }"
                        >
                          <el-input
                            v-model="item.f_Name"
                            :placeholder="$t('please enter')"
                          />
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item :label="$t('电话')" labelWidth="60px">
                          <el-input
                            v-model="item.f_Phone"
                            :placeholder="$t('please enter')"
                          />
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item :label="$t('职业')" labelWidth="60px">
                          <el-input
                            v-model="item.f_Post"
                            :placeholder="$t('please enter')"
                          />
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item :label="$t('部门')" labelWidth="60px">
                          <el-input
                            v-model="item.f_Dep"
                            :placeholder="$t('please enter')"
                          />
                        </el-form-item>
                      </el-col>
                    </div>
                  </div>
                </el-col>
              </div>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-form>
  </div>
</template>
<script>
const api = window.$api.erpcase.customer;

export default {
  props: {
    setBlock: {
      type: Boolean,
      default: true,
    },
    f_State: {},
  },
  data() {
    return {
      index_f_IsDefault: 0,
      moduleCode: "ERP_Customers",
      activeName: "tab0",
      formData: {
        case_erp_customer_f_Name: "", // 客户名称
        case_erp_customer_f_Type: "", // 客户类型
        case_erp_customer_f_Industry: "", // 所在行业
        case_erp_customer_f_Source: "", // 来源
        case_erp_customer_f_Scale: "", // 规模
        case_erp_customer_f_ComNature: "", // 公司性质
        case_erp_customer_f_Address: "", // 地址
        case_erp_customer_f_File: "", // 图片上传
        case_erp_customer_f_DefaultName: "", // 默认联系人姓名
        case_erp_customer_f_DefaultPhone: "", // 默认联系人手机号
        case_erp_customer_f_DefaultPost: "", // 默认联系人职位
        case_erp_customer_f_DefaultDep: "", // 默认联系人部门
      },
      rules: {
        case_erp_customer_f_Name: [
          { required: true, message: "please enter客户名称" },
        ],
        case_erp_customer_f_Type: [
          { required: true, message: "please select客户类型" },
        ],
        case_erp_customer_f_Industry: [
          { required: true, message: "please enter客户所在行业" },
        ],
        case_erp_customer_f_Source: [{ required: true, message: "please select来源" }],
        // phone: [
        //     { pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/, message: 'please enter电话/手机号' },
        // ],
      },
      case_erp_customercontacts_columns: [
        {
          id: "case_erp_customercontacts_F_Name",
          prop: "f_Name",
          label: "姓名",
          required: true,
        },
        {
          id: "case_erp_customercontacts_F_Phone",
          prop: "f_Phone",
          label: "电话",
          patterns: [
            {
              msg: "please enter电话号",
              reg: /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
            },
          ],
        },
        {
          id: "case_erp_customercontacts_F_Post",
          prop: "f_Post",
          label: "职位",
        },
        { id: "case_erp_customercontacts_F_Dep", prop: "f_Dep", label: "部门" },
        {
          id: "case_erp_customercontacts_F_IsDefault",
          prop: "f_IsDefault",
          label: "",
        },
      ],
      case_erp_customercontacts_data: [],
    };
  },
  computed: {
    case_erp_customer_f_TypeOptions() {
      return [
        { value: " 普通客户", label: " 普通客户" },
        { value: "重要客户", label: "重要客户" },
        { value: "SVIP客户", label: "SVIP客户" },
      ];
    },
    case_erp_customer_f_SourceOptions() {
      return [
        { value: "电话销售", label: "电话销售" },
        { value: "主动来电 ", label: "主动来电" },
        { value: "客户介绍", label: "客户介绍" },
        { label: "访问官网", value: "访问官网" },
        { label: "其他途径", value: "其他途径" },
      ];
    },
    case_erp_customer_f_ScaleOptions() {
      return [
        { value: "20人以下", label: "20人以下" },
        { value: "20~50人", label: "20~50人" },
        { value: "50~100人", label: "50~100人" },
        { label: "100~500人", value: "100~500人" },
        { label: "500人以上", value: "500人以上" },
      ];
    },
    case_erp_customer_f_ComNatureOptions() {
      this.lr_loadDataItem("CompanyNature");
      return this.lr_dataItemOptions(this.lr_dataItem["CompanyNature"]);
    },
    // case_erp_customercontacts_f_IsDefaultOptions() {
    //     return [{ "value": "0", "label": "默认" }]
    // },
  },
  methods: {
    //  默认联系人
    defaults() {
      this.formData.case_erp_customer_f_DefaultName =
        this.case_erp_customercontacts_data[this.index_f_IsDefault].f_Name; // 默认联系人姓名
      this.formData.case_erp_customer_f_DefaultPhone =
        this.case_erp_customercontacts_data[this.index_f_IsDefault].f_Phone; // 默认联系人手机号
      this.formData.case_erp_customer_f_DefaultPost =
        this.case_erp_customercontacts_data[this.index_f_IsDefault].f_Post;
      this.formData.case_erp_customer_f_DefaultDep =
        this.case_erp_customercontacts_data[this.index_f_IsDefault].f_Dep;
      let i = 0;

      for (i in this.case_erp_customercontacts_data) {
        if (i == this.index_f_IsDefault) {
          this.case_erp_customercontacts_data[i].f_IsDefault = "0"; //是
        } else {
          this.case_erp_customercontacts_data[i].f_IsDefault = "1"; // 否
        }
      }
      // console.log("是",this.case_erp_customercontacts_data)
    },
    handleCase_erp_customercontactsAddRow() {
      let point = {};
      point.f_Name = "";
      point.f_Phone = "";
      point.f_Post = "";
      point.f_Dep = "";
      point.f_IsDefault = "";
      this.case_erp_customercontacts_data.push(point);
    },
    handleCase_erp_customercontactsDeleteRow(event) {
      this.case_erp_customercontacts_data.splice(event.index, 1);
    },

    // Reset表单
    resetForm() {
      this.$formClear(this.$refs.form);
      this.activeName = "tab0";
      this.case_erp_customercontacts_data = [];
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }
      // if (!this.$refs.case_erp_customercontacts_table.validate()) {
      //     return false
      // }

      return true;
    },
    // 设置表单数据
    setForm(data) {
      this.$setFormData(
        this.formData,
        data.caseErpCustomerEntity,
        "case_erp_customer"
      );
      this.case_erp_customercontacts_data = data.caseErpCustomercontactsList;
      if (data.caseErpCustomercontactsList.length > 0) {
        for (let i in data.caseErpCustomercontactsList) {
          if (data.caseErpCustomercontactsList[i].f_IsDefault == "0") {
            this.index_f_IsDefault = i - 0;
            // console.log("第几条",this.index_f_IsDefault)
          }
        }
      }
    },
    // 获取表单数据
    getForm() {
      const formData = {};
      formData.caseErpCustomerEntity = this.$getFormData(
        this.formData,
        "case_erp_customer"
      );
      formData.caseErpCustomercontactsList =
        this.case_erp_customercontacts_data;
      return formData;
    },

    async loadFormData({ keyValue /*,params,node*/ }) {
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const data = await this.$awaitWraper(api.get(keyValue));
      if (data) {
        this.setForm(data);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点

      if (this.case_erp_customercontacts_data.length > 0) {
        this.defaults();
      }
      const postData = this.getForm();
      postData.caseErpCustomerEntity.f_State = this.f_State;
      if (isEdit) {
        // Edit

        return await this.$awaitWraper(api.update(keyValue, postData));
      } else {
        // Add
        postData.caseErpCustomerEntity.f_Id = keyValue;

        return await this.$awaitWraper(api.add(postData));
      }
    },
  },
};
</script>